package com.example.nettyandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

public class MainActivity extends AppCompatActivity {
    EditText et_scroll;
    EditText et_msg;
    Button btn_send, btn_pic;
    Activity activity;

    String host = "192.168.1.87";
    int port = 8080;

    NioEventLoopGroup group;
    Channel channel;
    ChannelFuture channelFuture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }
    void initView() {
        activity = this;
        connect(handler);

        et_scroll = (EditText) super.findViewById(R.id.et_scroll);
        et_msg = (EditText) super.findViewById(R.id.et_msg);
        btn_send = (Button) super.findViewById(R.id.btn_send);
        btn_pic = (Button) super.findViewById(R.id.btn_pic);

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = String.valueOf(et_msg.getText()) + "\r\n";
                if (msg.length() != 0) {
                    System.out.println("------ btn_send:"+msg);
                    handler.obtainMessage(0x03).sendToTarget();
                }
            }
        });

        btn_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
            }
        });
    }

    void connect(final Handler handler) {
        new Thread() {
            @Override
            public void run() {
                try {
                    group = new NioEventLoopGroup();
                    Bootstrap bootstrap = new Bootstrap();
                    bootstrap.group(group)
                            .channel(NioSocketChannel.class)
                            .option(ChannelOption.SO_KEEPALIVE, true)
                            .option(ChannelOption.TCP_NODELAY, true)
                            .handler(new ClientInitializer(handler));

                    channelFuture = bootstrap.connect(new InetSocketAddress(host, port));
                    channel = channelFuture.sync().channel();
                    channel.closeFuture().sync();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String m = msg.obj + "";
            switch (msg.what) {
                case 0x00:
                    String hello = new String("I'm in!");
                    et_scroll.setText(et_scroll.getText() + hello);
                    channel.writeAndFlush(hello);
                    break;
                case 0x01:
                    et_scroll.setText(et_scroll.getText() + m);
                    break;
                case 0x02:
                    et_msg.setText("");
                    break;
                case 0x03:
                    String mmm = String.valueOf(et_msg.getText() + "");
                    if (mmm.length() == 0) {
                        return;
                    }
                    channel.writeAndFlush(Unpooled.copiedBuffer(mmm, CharsetUtil.UTF_8))
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                                    handler.obtainMessage(0x02).sendToTarget();
                                }
                            });
                    break;
                default:
                    Toast.makeText(activity, "UNKNOWN MSG: " + m, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
